<%@page import="external.EmailUtility"%>
<%@page import="external.TokenGenerator"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<link rel="stylesheet" href="css/sidebar.css">
<% 
String token=request.getParameter("token");

String operation = request.getParameter("operation");

String user = request.getParameter("user_id");
int user_id = Integer.parseInt(user);
%>

 <%@ page import="servlets.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<%
						SessionFactory sessionFactory = HibernateUtil
								.getSessionAnnotationFactory();
						Session s = sessionFactory.openSession();
						s.beginTransaction();
						UserMethods um= new UserMethods();
							response.setContentType("text/html");

					%> 
					<div class="friends">

	<div style="text-align:center">
		<div class="title">
			<h3>	<%if("activate".equals(operation)){ %>
	Aktywacja konta
	<%}
	else if("delete".equals(operation)){
	%>
	Usuwanie konta
	<%}
	else if ("changeEmail".equals(operation))
	{
	%>
	Zmiana adresu E-mail
	<%} %></h3>
		</div>
	</div>
<div id="friends_content" style="width:100%;">
<%if("activate".equals(operation)){ 
	
	if(um.findTokenByOperationAndUser(sessionFactory, s, "activate", user_id).get(0).getToken().equals(token)){
		out.println("Aktywacja konta powiodła się. Mozesz się zalogować");
		s.delete(um.findTokenByOperationAndUser(sessionFactory, s, "activate", user_id).get(0));
		um.activateUser(sessionFactory, s, user_id);
	}
	else out.println("Aktywacja konta nie powiodła się.");
}

else if("delete".equals(operation)){
	if(um.findTokenByOperationAndUser(sessionFactory, s, "delete", user_id).get(0).getToken().equals(token)){
		out.println("Usunięcie konta powiodło się.");
		s.delete(um.findTokenByOperationAndUser(sessionFactory, s, "delete", user_id).get(0));
		um.delete(sessionFactory, s, um.id_to_user(sessionFactory, s, user_id));
	}
	else out.println("Usunięcie konta nie powiodło się.");
}

else if("changeEmail".equals(operation)){
	if(um.findTokenByOperationAndUser(sessionFactory, s, "changeEmail", user_id).get(0).getToken().equals(token)){
		String email = um.findTokenByOperationAndUser(sessionFactory, s, "changeEmail", user_id).get(0).getEmail();
		out.println("Kontaktowy adres email został zmieniony na: " + email);
		s.delete(um.findTokenByOperationAndUser(sessionFactory, s, "changeEmail", user_id).get(0));
		um.edit_email(sessionFactory, s, email, um.id_to_user(sessionFactory, s, user_id));
	}
	else out.println("Zmiana kontaktowego konta email nie powiodła się.");
	
}

else if("passwordReminder".equals(operation)){
	if(um.findTokenByOperationAndUser(sessionFactory, s, "passwordReminder", user_id).get(0).getToken().equals(token)){
	String password = TokenGenerator.generatePassword();
	out.println("Nowe haslo przeslano w kolejnej wiadomosci email");
	String passTresc = EmailUtility.sendPasswordMail(password, um.id_to_user(sessionFactory, s, user_id).getUsername());

		ServletContext context = getServletContext();
		String host = context.getInitParameter("host");
		String port = context.getInitParameter("port");
		String user1 = context.getInitParameter("user");
		String pass = context.getInitParameter("pass");
	
	s.delete(um.findTokenByOperationAndUser(sessionFactory, s, "passwordReminder", user_id).get(0));
	EmailUtility.sendEmail(host, port, user1, pass, um.id_to_user(sessionFactory, s, user_id).getEmail(), "[HexGames Social] Resetowanie hasła", passTresc);
	um.edit_password(sessionFactory, s, password,  um.id_to_user(sessionFactory, s, user_id));
	}
	else out.println("Resetowanie hasla nie powiodlo się.");
	
}
%>

</div>					
					<%	s.getTransaction().commit();	%>

</div>		
		
					
					